package com.sunjob.mroresumeproject.busresumecontroller;

import com.sunjob.common.domain.mroresume.BusAction;
import com.sunjob.common.domain.mroresume.WHBus;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.BusActionService;
import com.sunjob.mroresumeproject.service.WHBusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/WHBus")
@CrossOrigin
@Api(tags = "机车入库履历")
public class WHBusController {

    @Autowired
    WHBusService whBusService;

    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2){

        int page2=Integer.parseInt(pages2);
        if (page2<1)
        {
            page2=1;

        }

        int size=5;
        //部件定检履历查出部件定检记录总条数
        int count=whBusService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;

        if(page2>pageCount)
        {
            page2=pageCount;
        }

        //第一页放五条数据
        int pages=(page2-1)*size;
        List<WHBus> list=whBusService.queryWHBus(pages,size);


        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }


    //部件维修履历菜单查询
    @RequestMapping("/selectWHBus")
    @ApiOperation(value = "分页")
    public Result selectWHBus(){
        int page2=1;
        int size=5;

        //部件维修履历查出部件维修记录总条数
        int count= whBusService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<WHBus> list=whBusService.queryWHBus(pages,size);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectWHBuscontent")
    @ApiOperation(value = "条件查询")
    public Result selectWHBuscontent(@ApiParam(value = "Instance_id",required = false) String Instance_id){
        //搜索框条件查询可以是部件维修编号或者部件名称
        List<WHBus> list=whBusService.queryWHBus2(Instance_id);

        //result返回查询出的部件维修履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }





    //新增按钮，部件定维修履历新增
    @RequestMapping("/insertWHBus")
    @ApiOperation(value = "新增")
    public void insertBus(@RequestBody WHBus whBus){
        whBus.setStatus(1);
        whBusService.insertBus(whBus);

    }

    //新增
    @RequestMapping("/insertWHBus")
    public void insertWHBus(WHBus whBus){
        whBus.setStatus(1);
        whBusService.insertBus(whBus);
    }




    //编辑按钮，根据checkid查询出当前id的数据
    @RequestMapping("returnWHBus")
    @ApiOperation(value = "查询")
    public Result returnWHBus(@RequestParam("Instance_id")Long Instance_id){
        List<WHBus> whBuses=whBusService.queryWHBusone(Instance_id);
        Result result=new Result();
        result.setData(whBuses);
        return result;
    }

    //对根据checkid查询出的部件维修履历数据做修改
    @RequestMapping("/updateWHBus")
    @ApiOperation(value = "修改")
    public void updateWHBus(WHBus whBus){

        whBusService.updateWHBus(whBus);

    }

    //根据repairid删除修改这条记录的状态
    @RequestMapping("/deleteWHBus")
    @ApiOperation(value = "删除")
    public void deleteWHBus(@RequestParam("Instance_id")String Instance_id){
        //修改这条记录状态，0为消失，1为存在
       whBusService.deleteWHBus(Instance_id);


    }

    @RequestMapping("/deleteWHBus2")
    public void deleteWHBus2(String Instance_id){
        System.out.println(Instance_id);
        String[] a= Instance_id.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            whBusService.deleteWHBusone(a[i]);
        }
    }

}
